package com.boho.estates.data.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.util.Date;

import cn.boho.framework.po.DynaBeanMap;
import cn.boho.framework.service.MessageService;
import cn.boho.framework.utils.DateUtils;

public class DataService {

	private static final String CHARSET = "UTF8";

	/**
	 * 备份数据库
	 */
	public static void backup() throws Exception{

		InputStream in = null;
		InputStreamReader isr = null;

		BufferedReader br = null;

		FileOutputStream fos = null;
		OutputStreamWriter writer = null;

		try {
			Runtime rt = Runtime.getRuntime();
			String commond = "cmd /c mysqldump -u"+MessageService.getMessage("db_username")+" -p"+MessageService.getMessage("db_password")+" --default-character-set=utf8 "+ MessageService.getMessage("db_sid");
			final Process process = rt.exec(commond, null, new File(MessageService.getMessage("mysql_install_path")));
			in = process.getInputStream();
			isr = new InputStreamReader(in, CHARSET);

			new Thread(new Runnable() {
		    	public void run() {
		    		BufferedReader r = new BufferedReader(new InputStreamReader(process.getErrorStream()));
		    		try {
		    			while (r.readLine() != null);
					} catch (IOException e) {
		    		} finally {
		    			try {
		    				r.close();
		    			} catch (IOException e) {
			    		}
		    		}
				}
			}).start();
			
			String inStr;
			StringBuilder sb = new StringBuilder();
			String outStr;

			br = new BufferedReader(isr);
			while ((inStr = br.readLine()) != null) {
				sb.append(inStr + "\r\n");
			}
			outStr = sb.toString();

			fos = new FileOutputStream(MessageService.getMessage("backup_path"));
			writer = new OutputStreamWriter(fos, CHARSET);
			writer.write(outStr);
			writer.flush();
		} catch (Exception e) {
			throw e;
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}

	}

	/**
	 * 恢复数据库
	 * @throws IOException
	 */
	public static void restore() throws Exception {
		try {
			Runtime rt = Runtime.getRuntime();
			String commond = "cmd /c mysql -u"+MessageService.getMessage("db_username")+" -p"+MessageService.getMessage("db_password")+" --default-character-set=utf8 "+ MessageService.getMessage("db_sid") + "<"+MessageService.getMessage("backup_path");
			final Process process = rt.exec(commond, null, new File(MessageService.getMessage("mysql_install_path")));
			new Thread(new Runnable() {
		    	public void run() {
		    		BufferedReader r = new BufferedReader(new InputStreamReader(process.getInputStream()));
		    		try {
		    			while (r.readLine() != null);
					} catch (IOException e) {
		    		} finally {
		    			try {
		    				r.close();
		    			} catch (IOException e) {
			    		}
		    		}
				}
			}).start();
			
			new Thread(new Runnable() {
		    	public void run() {
		    		BufferedReader r = new BufferedReader(new InputStreamReader(process.getErrorStream()));
		    		try {
		    			while (r.readLine() != null);
					} catch (IOException e) {
		    		} finally {
		    			try {
		    				r.close();
		    			} catch (IOException e) {
			    		}
		    		}
				}
			}).start();
		} catch (Exception e) {
			throw e;
		}
	}
	
	public static DynaBeanMap getBackupFile(){
		DynaBeanMap dbm = null;
		File file = new File(MessageService.getMessage("backup_path"));
		if(file.exists()){
			dbm = new DynaBeanMap();
			dbm.put("filename", file.getName());
			dbm.put("filesize", getFileSize(file.length()));
			dbm.put("date", DateUtils.format(new Date(file.lastModified())));
		}
		return dbm;
	}
	
	private static String getFileSize(long size){
		DecimalFormat df = new DecimalFormat("0.## "); 
		Float si = size / 1024f / 1024f;
		return df.format(si)+"MB";
	}
	
	/**
	 * 清理系统缓存
	 */
	public static void clearCache(){
		
	}
	

	public static void main(String[] args) throws IOException {
//		backup();
//		restore();
	}
}
